<template>
  <div>
    <BaseFormDialog :title="IsAddType ? '新增数据字典':'修改数据字典'" :visible="visible" @SureFormDialog="Sure"
                    @CancelFormDialog="CancelFromDialog"
                    @DialogVisibleListener="DialogVisibleListener">
      <template #FormContent>
        <el-form label-width="100px" v-model="ModelData">
          <el-form-item label="名称">
            <el-input type="text" name="name" v-model="ModelData.name"/>
          </el-form-item>
          <el-form-item label="值">
            <el-input type="text" name="value" v-model="ModelData.value"/>
          </el-form-item>
          <el-form-item label="类型">
            <el-input type="text" name="type" v-model="ModelData.type"/>
          </el-form-item>
          <el-form-item label="备注">
            <el-input type="text" name="remark" v-model="ModelData.remark"/>
          </el-form-item>
        </el-form>
      </template>
    </BaseFormDialog>
  </div>
</template>

<script setup>
import BaseFormDialog from "@/components/BaseFormDialog/Index.vue"
import {computed, onMounted, reactive} from "vue";
import {AddDictionary, UpdateDictionary} from "@/api/DataDictionary";
import {ElMessage} from "element-plus";
import {SetObjectPropertyNormal} from "@/utils/ObjectPropertyHelper";

const DataDictionaryEmit = defineEmits(['SureFormDialog', 'CancelFormDialog', 'DialogVisibleListener', 'UpdateDataEvent'])
const DataDictionaryDialogConfig = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  title: {
    type: String,
    default: ''
  },
  IsAddType: {
    type: Boolean,
    default: true
  },
  Model: {
    type: Object,
    default: reactive({
      name: '',
      value: '',
      type: '',
      remark: ''
    })
  }
})

const ModelData = computed(() => DataDictionaryDialogConfig.Model);

function Sure() {
  if (DataDictionaryDialogConfig.IsAddType) {
    AddDictionaryHandler();
  } else {
    UpdateDictionaryHandler();
  }
  DataDictionaryEmit('SureFormDialog');
}

function CancelFromDialog() {
  DataDictionaryEmit('CancelFormDialog');
}

function DialogVisibleListener(visible) {
  if (DataDictionaryDialogConfig.IsAddType) {
    SetObjectPropertyNormal(DataDictionaryDialogConfig.Model);
  }
  DataDictionaryEmit('DialogVisibleListener', visible);
}

function AddDictionaryHandler() {
  AddDictionary(ModelData.value).then(response => {
    if (response.code === 200) {
      ElMessage({
        message: '添加成功',
        type: 'success',
        duration: 1000
      });
      SetObjectPropertyNormal(ModelData.value);
      DataDictionaryEmit('UpdateDataEvent');
    } else {
      ElMessage({
        message: response.msg,
        type: 'warning',
        duration: 1000
      })
    }
  })
}

function UpdateDictionaryHandler() {
  UpdateDictionary(ModelData.value).then(response => {
    if (response.code === 200) {
      ElMessage({
        message: '修改成功',
        type: 'success',
        duration: 1000
      })
      DataDictionaryEmit('UpdateDataEvent');
    } else {
      ElMessage({
        message: response.msg,
        type: 'warning',
        duration: 1000
      })
    }
  })
}


</script>

<style scoped>

</style>